package com.qibo.receipt.fragment;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qibo.baselib.base.BaseFragment;
import com.qibo.baselib.utils.Arith;
import com.qibo.receipt.R;

import java.text.DecimalFormat;

import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * 收款码
 */
public class CollectionCodeFragment extends BaseFragment{
    private ImageView mEnglishIv;
    private TextView frag_collectioncode_amount;
    @Override
    protected int getInflaterLayoutId() {
        return R.layout.receiptmodule_frag_collectioncode;
    }

    @Override
    protected void initView(View view) {
        mEnglishIv = view.findViewById(R.id.mEnglishIv);
        frag_collectioncode_amount = view.findViewById(R.id.frag_collectioncode_amount);
    }

    @Override
    protected void initAction() {
        frag_collectioncode_amount.setText("￥"+ Arith.format(getActivity().getIntent().getStringExtra("amount")));
    }

    @Override
    protected void initData() {
        createEnglishQRCode();
    }

    private void createEnglishQRCode() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return QRCodeEncoder.syncEncodeQRCode("200", BGAQRCodeUtil.dp2px(getActivity(), 150), Color.BLACK);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    mEnglishIv.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(getActivity(), "生成英文二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }
}
